package atm;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
                                                    //转账
public class TransferPanel extends JPanel   implements ActionListener {
    private ATMFrame atmFrame;
    private ImageIcon sure = new ImageIcon("images/buttonSure.jpg");
    private JLabel transMoney = new JLabel("转移金额：");
    private JLabel transUser = new JLabel("转移账户：");

    private JTextField moneyField = new JTextField();
    private JTextField userField = new JTextField();
    private JButton sureBtn = new JButton(sure);
    private Font font = new Font("黑体", Font.BOLD, 20);

    public TransferPanel(ATMFrame atmFrame) {
        this.atmFrame=atmFrame;
        this.setLayout(null);
        this.add(sureBtn);
        this.add(transMoney);
        this.add(moneyField);
        this.add(transUser);
        this.add(userField);
        transMoney.setBounds(300, 200, 120, 50);
        transUser.setBounds(300, 260, 140, 50);
        sureBtn.setBounds(400, 400, 140, 50);
        moneyField.setBounds(400, 200, 140, 50);
        userField.setBounds(400, 260, 140, 50);
        transMoney.setFont(font);
        transUser.setFont(font);
        sureBtn.addActionListener(this);

        this.setOpaque(false);


    }
    public void actionPerformed(ActionEvent e) {
        System.out.println("转账点击"+e.getActionCommand());
        if( e.getSource() == sureBtn){

           // User user1=atmFrame.getLoginUser();
            String  trMoney =moneyField.getText();
            String trUser =  userField.getText();
            SuperArray users=atmFrame.getUsers();
            for(int i=0;i<users.size();i++){
                User user = (User) users.get(i);
                if(trUser.equals(user.getAccount())){
//                    System.out.println("余额");
                    if(atmFrame.getLoginUser().getBlance()<Double.parseDouble( moneyField.getText())){
                        System.out.println("余额不足");
                        return;
                    }


                    try{
                        atmFrame.getLoginUser().setBlance(atmFrame.getLoginUser().getBlance()-Double.parseDouble(trMoney));
                        System.out.println(atmFrame.getLoginUser().getAccount()+atmFrame.getLoginUser().getBlance()+"");
                        user.setBlance(user.getBlance() + Double.parseDouble( moneyField.getText()));

                        System.out.println(user.getAccount()+user.getBlance()+"");
                    }catch (NumberFormatException e1){
                        JOptionPane.showMessageDialog(this,"必须输入数字");
                        return;
                    }


                    atmFrame.switchPanel("menu");
                }

            }
        }
    }
}